package com.hongdee.crm.tdc.mapper;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zhangkai
 * @desc 一句话描述
 **/
public class MarkertQuestionSQL {
    /***
     * 查询问题库列表
     */
    public String queryLibrarySQL(Map param) {
        return new SQL() {{
            SELECT(" * ");
            FROM(" t_market_dc_question_library ");
            WHERE("p_id = #{pId}");

            Object code = param.get("brandCode");
            if (code != null && !StringUtils.isEmpty(code.toString())) {
                WHERE("brand_code = #{brandCode}");
            }

            Object isTrue = param.get("isTrue");
            if (isTrue != null && Boolean.parseBoolean(isTrue.toString())) {
                WHERE("label_type  in ('0','1','问题分类','问题项') ");
            }

            Object status = param.get("status");
            if (status != null && !StringUtils.isEmpty(status)) {
                WHERE(" status = #{status}  ");
            }
            ORDER_BY("dis_order asc ");
        }}.toString();
    }


    /***
     * 新增数据项字典
     * @return
     */
    public String addLibrarySQL() {
        return new SQL() {{
            INSERT_INTO(" t_market_dc_question_library ");
            INTO_COLUMNS("id,label,label_type,value_type,p_id,dis_order,status,brand_code");
            INTO_VALUES("#{id},#{label},#{labelType},#{valueType},#{pId},#{disOrder},#{status},#{brandCode}");
        }}.toString();
    }

    /***
     * 编辑数据项字典
     * @return
     */
    public String updateLibrarySQL() {
        return new SQL() {{
            UPDATE("t_market_dc_question_library");
            SET(" label=#{label},label_type=#{labelType},value_type=#{valueType},dis_order=#{disOrder},status=#{status},brand_code=#{brandCode} ");
            WHERE("id = #{id}");

        }}.toString();
    }

    /***
     * 编辑数据项状态
     * @return
     */
    public String updateLibraryStatusSQL() {
        return new SQL() {{
            UPDATE("t_market_dc_question_library");
            SET("status = #{status}");
            WHERE("id = #{id}");

        }}.toString();
    }

    /***
     * 判断是否已存在对应的记录
     * @return
     */
    public String existLibrarySQL(Map params) {
        return new SQL() {{
            SELECT(" * ");
            FROM("t_market_dc_question_library");
            WHERE(" label = #{label}");
            WHERE(" p_id = #{pId}");
            if (params.get("id") != null && !StringUtils.isEmpty(params.get("id").toString())) {
                WHERE("id != #{id} ");
            }

        }}.toString();
    }

    /**
     * 查询标签详情
     *
     * @return
     */
    public String queryLibraryInfoSQL() {
        return new SQL() {{
            SELECT(" * ");
            FROM("t_market_dc_question_library");

            WHERE(" id = #{id}");


        }}.toString();
    }

    /**********************调研问卷基础总结********************************/
    /***
     * 查询问券列表
     * @param params
     * @return
     */
    public String queryNaireSQL(Map params) {
        return new SQL() {{
            SELECT(" * ");
            FROM("t_market_dc_question_naire");
            WHERE(" company_id = #{companyId}");

            if (!StringUtils.isEmpty(params.get("code"))) {
                WHERE(" code like '%" + params.get("code").toString() + "%'");
            }


        }}.toString();
    }

    /***
     * 查询问券详情
     * @return
     */
    public String queryNaireInfoSQL() {
        return new SQL() {{
            SELECT(" * ");
            FROM("t_market_dc_question_naire");
            WHERE(" id = #{id}");
        }}.toString();
    }

    /***
     * 通过编码、公司Id 查询记录详情
     * @return
     */
    public String queryNaireInfoByCodeSQL() {
        return new SQL() {{
            SELECT(" * ");
            FROM(" t_market_dc_question_naire_info ");
            WHERE("company_id = #{companyId}");
            WHERE("code = #{code}");

        }}.toString();
    }

    /***
     * 更新问卷状态
     * @return
     */
    public String updateNaireStatusSQL() {
        return new SQL() {{
            UPDATE("t_market_dc_question_naire");
            SET(" status =#{status}");
            WHERE(" id = #{id}");
        }}.toString();
    }

    /***
     * 更新问卷属性
     * @return
     */
    public String updateNaireSQL() {
        return new SQL() {{
            UPDATE("t_market_dc_question_naire");
            SET(" name=#{name},type=#{type},total_score=#{totalScore},update_time = #{updateTime},update_user = #{updateUser} ");
            WHERE("id = #{id}");
        }}.toString();
    }

    /***
     * 新增基础问券
     * @return
     */
    public String insertNaireSQL() {
        return new SQL() {{
            INSERT_INTO("t_market_dc_question_naire");
            INTO_COLUMNS("id,code,name,type,status,company_id,total_score,create_time,create_user");
            INTO_VALUES("#{id},#{code},#{name},#{type},#{status},#{companyId},#{totalScore},#{createTime},#{createUser}");
        }}.toString();
    }

    /*********问券明细***********/

    public String deleteNarireItemSQL() {
        return new SQL() {{
            DELETE_FROM("t_market_dc_question_naire_info");
            WHERE(" narie_code = #{naireCode}");
        }}.toString();
    }

    /****
     * 新增试券明细
     */
    public String insertNarireItemSQL() {
        return new SQL() {{
            INSERT_INTO("t_market_dc_question_naire_info");
            INTO_COLUMNS("id,narie_code,library_id,assgin_score,company_id,label,value_type,label_type,p_id");
            INTO_VALUES("#{id},#{narieCode},#{libraryId},#{assginScore},#{companyId},#{label},#{valueType},#{labelType},#{pId}");
        }}.toString();
    }

    /**
     * 更新试券分值
     *
     * @return
     */
    public String updateNarireItemScoreSQL() {
        return new SQL() {{
            UPDATE("t_market_dc_question_naire_info");
            SET("assgin_score = #{score}");
            WHERE(" id  = #{id}");
        }}.toString();
    }


    /**
     * 查询问券详情记录
     */
    public String queryNaireItemSQL(Map params) {
        return new SQL() {{
            SELECT(" * ");
            FROM(" t_market_dc_question_naire_info ");
            WHERE("company_id = #{companyId}");
            WHERE("narie_code = #{narieCode}");
            WHERE("p_id = #{pId}");
        }}.toString();
    }

    /***
     * 计算问题项分值
     */
    public String countItemScoreSQL() {

        String tempQuerySql = new SQL() {{

            SELECT("MAX(assgin_score) AS score, p_id, company_id, narie_code ");
            FROM(" t_market_dc_question_naire_info ");
            WHERE("company_id =#{companyId}");
            WHERE("narie_code =#{code}");
            WHERE(" label_type = '评分项' ");
            GROUP_BY(" company_id, p_id, narie_code ");

        }}.toString();

        return new SQL() {{

            UPDATE(" t_market_dc_question_naire_info t1, (" + tempQuerySql + ") t2");
            SET(" t1.assgin_score = t2.score ");
            WHERE(" t1.company_id = t2.company_id ");
            WHERE(" t1.narie_code = t2.narie_code");
            WHERE(" t1.`library_id` = t2.p_Id ");
            WHERE(" t1.company_id =#{companyId}");
            WHERE(" t1.narie_code =#{code}");
            WHERE(" label_type = '问题项' ");

        }}.toString();
    }

    /***
     * 计算问题分类分值
     */
    public String countItemGroupScoreSQL() {

        String tempQuerySql = new SQL() {{

            SELECT("SUM(assgin_score) AS score, p_id, company_id, narie_code ");
            FROM(" t_market_dc_question_naire_info ");
            WHERE("company_id =#{companyId}");
            WHERE("narie_code =#{code}");
            WHERE(" label_type = '问题项' ");
            GROUP_BY(" company_id, p_id, narie_code ");

        }}.toString();

        return new SQL() {{

            UPDATE(" t_market_dc_question_naire_info t1, (" + tempQuerySql + ") t2");
            SET(" t1.assgin_score = t2.score ");
            WHERE(" t1.company_id = t2.company_id ");
            WHERE(" t1.narie_code = t2.narie_code");
            WHERE(" t1.`library_id` = t2.p_Id ");
            WHERE(" t1.company_id =#{companyId}");
            WHERE(" t1.narie_code =#{code}");
            WHERE(" label_type = '问题分类' ");

        }}.toString();
    }

    /***
     * 计算问题分类分值
     */
    public String queryTotalScoreSQL() {

        String tempQuerySql = new SQL() {{

            SELECT("SUM(assgin_score) AS score, p_id, company_id, narie_code ");
            FROM(" t_market_dc_question_naire_info ");
            WHERE("company_id =#{companyId}");
            WHERE("narie_code =#{code}");
            WHERE(" label_type = '问题分类' ");
            GROUP_BY(" company_id, p_id, narie_code ");

        }}.toString();

        return tempQuerySql;
    }
}
